const { Op, Sequelize } = require("sequelize");
const Community = require('../model/community.model');
const CommunityAttention = require('../model/community_attention.model')
const CommunityConversation = require('../model/community_conversation.model')
const moment = require('moment');
const seq = require('../db/seq')
class communityService {
    async saveCommunityService(param) {
        const { community_name, community_detail, creator, user_id, is_delete } = param;
        console.log(param, '----')
        const where = {
            community_name: { [Op.eq]: community_name }
        }

        const res = await Community.findOne({
            where
        })
        console.log(1111)
        // 没有查到
        if (!res) {
            console.log(1112222)
            const data = await Community.create({ community_name, community_detail, creator, user_id, is_delete })
            return 'success'
        } else {
            console.log(1112222)
            // 查到之后更改
            const data = await Community.update({ community_detail, creator, user_id, is_delete }, {
                where: { community_name }
            })
            return 'success'
        }
        return 'error'
    }
    async getCommunityService(param) {
        const { community_name = '', community_detail = '', creator, user_id = '', is_delete, pageSize, currentPage } = param
        const where = { is_delete: 0 }
        community_name && (where['community_name'] = community_name)
        creator && (where['creator'] = creator)
        const { count, rows } = await Community.findAndCountAll({
            offset: (currentPage - 1) * pageSize,
            limit: Number(pageSize),
            where
        });
        console.log(rows)
        return rows ? { data: rows, total: count } : { error: 500 }
    }
    async getCommunityAttentionService(param) {
        const { user_id } = param
        const res = await CommunityAttention.findAll({
            where: { user_id }
        });
        console.log(res)
        return { data: res }
    }
    async saveCommunityAttentionService(param) {
        const { user_id, username, community_name, community_id } = param
        console.log(user_id, username, community_name, community_id,'------')

        const res = await CommunityAttention.create({ user_id, username, community_name, community_id })
        return res ? 'success' : 'error'
    }
    async getComConversationService(param) {
        const { community_id } = param

        const res = await CommunityConversation.findAll({
            where: { community_id },
            order: [
                ['createdAt', 'ASC'],
            ],
        });
        console.log(res, '🐷')
        return { data: res }
    }
    async saveComConversationService(param) {
        const { user_id, username, avator, community_id, content } = param
        const res = await CommunityConversation.create({ user_id, username, avator, community_id, content })
        console.log(res, '1111')
        return res ? 'success' : 'error'
    }
    async getCommunityUserNumService(param) {
        let result
        try {
            //   seq是定义的数据库实例  必须加await
            await seq.query(`(SELECT COUNT(DISTINCT(user_id)) AS count FROM CommunityAttentions)`, { type: seq.QueryTypes.SELECT }).then((res) => {
                result = res
                console.log(res)
            })
            return result

        } catch (err) {
            console.log(err)
            return err
        }
    }

}
// 导出后 在控制器controller中使用
module.exports = new communityService()